package com.example.administrator.dc.fragment;


import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.dc.Bean.Regresult;
import com.example.administrator.dc.R;
import com.example.administrator.dc.Service.RegService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class RegFragmenttwo extends BaseFragment {
    private EditText password;
    private EditText Confirmpassword;
    private Button submit;

    public void onViewCreated(View view,@Nullable Bundle savedInstanceState) {
        password= (EditText) view.findViewById(R.id.editText6);
        Confirmpassword= (EditText) view.findViewById(R.id.editText9);
        submit= (Button) view.findViewById(R.id.button4);
        Retrofit retrofit=new Retrofit.Builder().baseUrl("http://118.89.227.169/home/Api/").
                addConverterFactory(GsonConverterFactory.create()).build();
        final RegService retrofitSerivce= retrofit.create(RegService.class);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password1=password.getText().toString();
                final String Confirmpassword1=Confirmpassword.getText().toString();
                if(password.getText().toString().equals(Confirmpassword.getText().toString())){
                    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                    builder.setTitle("提示框");
                    builder.setMessage("手机号："+phone+"\n"+"密码："+password1+"确定注册吗");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Call<Regresult> call=retrofitSerivce.regresult(phone,Confirmpassword1,safecode);
                            call.enqueue(new Callback<Regresult>() {
                                @Override
                                public void onResponse(Call<Regresult> call, Response<Regresult> response) {
                                    if(response.body().getStatus()==1){
                                        Toast.makeText(getContext(),"注册成功",Toast.LENGTH_LONG).show();

                                    }

                                }

                                @Override
                                public void onFailure(Call<Regresult> call, Throwable t) {

                                }
                            });


                        }
                    });
                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {

                     }
                 });

                    AlertDialog dialog = builder.create();
                    dialog.show();




                }
                else {
                    Toast.makeText(getContext(),"密码不批对请重新输入",Toast.LENGTH_LONG).show();

                }

            }
        });



    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_reg_fragmenttwo, container, false);
    }

}
